package com.ikas.ai.server.config.executor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Value("${async.corePoolSize:10}")
    private int corePoolSize = 10;
    @Value("${async.maxPoolSize:200}")
    private int maxPoolSize = 200;
    @Value("${async.queueCapacity:10}")
    private int queueCapacity = 10;

    @Bean(name = "schedule")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("schedule-executor-");
        executor.initialize();
        return executor;
    }

    @Bean(name = "kafka-work-flow")
    public ThreadPoolTaskExecutor kafkaFlowExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("kafka-workflow-exec-");
        executor.setKeepAliveSeconds(60);
        executor.initialize();
        return executor;
    }
}